package com.fsoft.core.common.base;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.fsoft.core.shiro.ShiroContext;
import com.fsoft.core.utils.FileManage;
import com.fsoft.core.utils.OgnlUtils;

/**
 * F-Soft 基础Controller
 * @author Fish
 * @package com.fsoft.core.common.base
 * @email it.fish2010@foxmail.com
 * @date 2019-11-02
 * @CopyRight © F-Soft
 **/
public abstract class BaseController {

	@Autowired
	protected HttpServletRequest request; // 这里可以获取到request
	@Autowired
	protected HttpServletResponse response;

	/***
	 * F-Soft 获取当前登录用户信息
	 * @author Fish(it.fish2010 @ foxmail.com)
	 * @date 2019-11-02
	 * @return
	 */
	protected final UserVo getUser() {
		return ShiroContext.getCurrentUser();
	}

	/***
	 * F-Soft 当前登录用户的ID
	 * @author Fish(it.fish2010 @ foxmail.com)
	 * @date 2019-11-02
	 * @return
	 */
	protected final String getUserId() {
		return getUser().getId();
	}

	/***
	 * F-Soft 当前登录用户的登录帐号
	 * @author Fish(it.fish2010 @ foxmail.com)
	 * @date 2019-11-02
	 * @return
	 */
	protected final String getLoginAcct() {
		return getUser().getLoginAcct();
	}

	/**
	 * F-Soft 上传文件通用函数(本地服务器)
	 * @author it_software(it.fish2010@foxmail.com)
	 * @date 2020-02-29
	 * @param workSpace 文件夹目录
	 * @param fileName 重命名后的文件名
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	protected List<Map<String, String>> commonsUploadEx(File workSpace, String fileName, HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<Map<String, String>> ret = new ArrayList<Map<String, String>>();
		// 创建多部件解析器
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
		// 检测是否有文件上传
		if (multipartResolver.isMultipart(request)) {
			// 将 HttpServletRequest 造型为 MultipartHttpServletRequest
			MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
			// 获取请求中所有的文件名称
			Iterator<String> iterator = multiRequest.getFileNames();
			while (iterator.hasNext()) {
				// 获取上传的文件
				MultipartFile multiFile = multiRequest.getFile(iterator.next());
				if (multiFile != null) {
					// 文件扩展名
					String extension = OgnlUtils.getFileNameExt(multiFile.getOriginalFilename());
					// 随机文件名
					String newFName = null;
					if (StringUtils.isNotBlank(fileName)) {
						newFName = fileName.endsWith(extension) ? fileName : fileName + "." + extension;
					} else {
						newFName = OgnlUtils.randomLowerCaseId() + "." + extension;
						workSpace = FileManage.createSpace(workSpace, newFName.substring(0, 2));
					}
					File saveFile = FileManage.createFile(workSpace, newFName);
					multiFile.transferTo(saveFile);
					Map<String, String> rs = new HashMap<String, String>();
					rs.put("name", multiFile.getOriginalFilename());
					rs.put("webPath", FileManage.formatWebPath(saveFile.getAbsolutePath()));
					rs.put("fullPath", saveFile.getPath());
					ret.add(rs);
				}
			}
		}
		return ret;
	}
}
